﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.Da.Db.POS;
using Me.Amon.POS.C;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class Vips : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var dba = new RDBEngine();
            ListLevel(dba);

            var lev = Request["lev"];
            if (!CharUtil.IsValidateHash(lev))
            {
                lev = "1000000000000000";
            }
            LbLevel.SelectedValue = lev;

            dba.Reset();
            ListVip(dba, user.CorpId, lev, 0);
        }

        protected void LbCat_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListVip(new RDBEngine(), WebUtil.CurrentUser(Session).CorpId, LbLevel.SelectedValue, 0);
        }

        #region 导航
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BnAppendVip_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/POS/UpdateVip.aspx?lev=" + LbLevel.SelectedValue);
        }
        protected void BnPoints_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/POS/UpdatePoint.aspx?lev=" + LbLevel.SelectedValue);
        }
        #endregion

        private void ListLevel(RDBEngine dba)
        {
            dba.AddTable(DCustomerLevel.TABLE);
            dba.AddColumn(DCustomerLevel.ID);
            dba.AddColumn(DCustomerLevel.NAME);
            dba.AddSort(DCustomerLevel.POINT, true);

            LbLevel.DataSource = dba.ExecuteSelect();
            LbLevel.DataValueField = DCustomerLevel.ID;
            LbLevel.DataTextField = DCustomerLevel.NAME;
            LbLevel.DataBind();
        }

        private void ListVip(RDBEngine dba, string corp, string lev, int page)
        {
            dba.AddTable(DVip.TABLE);
            dba.AddColumn(DVip.ID);
            dba.AddColumn(DVip.CODE);
            dba.AddColumn(DVip.DISPLAY_NAME);
            dba.AddColumn(DVip.SEX);
            dba.AddColumn(DVip.LEVEL_ID);
            dba.AddColumn(DVip.LEVEL_NAME);
            dba.AddColumn(DVip.BIRTHDAY);
            dba.AddColumn(DVip.POINT);
            dba.AddColumn(DVip.CELLPHONE);
            dba.AddColumn(DVip.TELEPHONE);
            dba.AddWhere(DVip.CORP_ID, corp);
            dba.AddWhere(DVip.CHANNEL, CVip.CHANNEL);
            dba.AddWhere(DVip.LEVEL_ID, lev);

            var table = dba.ExecuteSelect();
            int cnt = table.Rows.Count;
            //if (cnt < 1)
            //{
            //    RpVips.DataSource = table;
            //    RpVips.DataBind();
            //    return;
            //}

            // 每页行数
            int RPP = 20;
            int num = (cnt - 1) / RPP;
            int p = page - 1;
            if (p + 2 > num)
            {
                p = num - 2;
            }
            if (p < 0)
            {
                p = 0;
            }

            HlHome.Enabled = num > 0 && page > 0;
            LlPrev.Visible = num > 3 && page > 0;

            HlPage1.CommandArgument = p.ToString();
            HlPage1.ForeColor = p == page ? Color.Red : Color.Blue;
            p += 1;
            HlPage1.Text = p.ToString();

            if (num > 0)
            {
                HlPage2.Visible = true;
                HlPage2.CommandArgument = p.ToString();
                HlPage2.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage2.Text = p.ToString();
            }
            else
            {
                HlPage2.Visible = false;
            }

            if (num > 1)
            {
                HlPage3.Visible = true;
                HlPage3.CommandArgument = p.ToString();
                HlPage3.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage3.Text = p.ToString();
            }
            else
            {
                HlPage3.Visible = false;
            }

            LlNext.Visible = num > 3 && page < num - 3;
            HlLast.Enabled = page < num;
            HlLast.CommandArgument = num.ToString();
            LlPage.Text = string.Format("第 {0}/{1} 页", page + 1, num + 1);

            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = table.DefaultView;
            pds.AllowPaging = true;//允许分页
            pds.PageSize = RPP;//单页显示项数
            pds.CurrentPageIndex = page;

            RpVips.DataSource = pds;
            RpVips.DataBind();
        }

        #region 分页
        protected void HlHome_Click(object sender, EventArgs e)
        {
            var dba = new RDBEngine();
            var lev = LbLevel.SelectedValue;
            ListVip(dba, WebUtil.CurrentUser(Session).CorpId, lev, 0);
        }

        protected void HlPage1_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage1.CommandArgument);
            var dba = new RDBEngine();
            var lev = LbLevel.SelectedValue;
            ListVip(dba, WebUtil.CurrentUser(Session).CorpId, lev, max);
        }

        protected void HlPage2_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage2.CommandArgument);
            var dba = new RDBEngine();
            var lev = LbLevel.SelectedValue;
            ListVip(dba, WebUtil.CurrentUser(Session).CorpId, lev, max);
        }

        protected void HlPage3_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage3.CommandArgument);
            var dba = new RDBEngine();
            var lev = LbLevel.SelectedValue;
            ListVip(dba, WebUtil.CurrentUser(Session).CorpId, lev, max);
        }

        protected void HlLast_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlLast.CommandArgument);
            var dba = new RDBEngine();
            var lev = LbLevel.SelectedValue;
            ListVip(dba, WebUtil.CurrentUser(Session).CorpId, lev, max);
        }
        #endregion
    }
}